package finassist.view;

import java.util.ArrayList;
import java.util.Date;;;;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.struts2.interceptor.SessionAware;

import finassist.controller.EntityProcessManager;
import finassist.controller.LoginUserManager;
import finassist.model.Entiti;
import finassist.model.Ledger;
import finassist.model.User;
import finassist.util.Utilities;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * AJ Projects
 * UserLoginAction.java
 * Purpose: Action class for User Login Module. This class takes care of all login related actions.
 *
 * @author Karthick Natarajan
 * @version 1.0 9/10/13
 */

public class EntityManageAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 9149826260758390091L;
	private Entiti entiti;
	private Ledger ledger;
	public Ledger getLedger() {
		return ledger;
	}

	public void setLedger(Ledger ledger) {
		this.ledger = ledger;
	}

	private Long id;
	private Map session;
	private EntityProcessManager linkController;
	private String role;
	private List<Ledger> ledgerList = new ArrayList<Ledger>();
	
	private List<String> ledgerListNames = new ArrayList<String>();
	private Map<String, List<String>> ledgerMap = new HashMap<String, List<String>>();
	

	

	private int screenName=0;
	

	public EntityManageAction() {
		linkController = new EntityProcessManager();
	}

	public String execute() {
		
		User user1 = (User) ActionContext.getContext().getSession().get("sesUser");
		role = user1.getRole();
this.ledgerList = linkController.listLedger();
		
		this.ledgerMap =  linkController.listLedgerType();
		
		
			return SUCCESS;
	}

	public String createEntity() {
	
		Utilities utilities =  new Utilities();
		entiti = getEntiti();
		System.out.println(getEntiti());
	
		try {
					
			User user1 = (User) ActionContext.getContext().getSession().get("sesUser");
			role = user1.getRole();
			entiti.setCreatedBy(user1.getUsername());
			entiti.setCreateDate(utilities.currentDateTime());
		linkController.createEntity(entiti); 
		ActionContext.getContext().getSession().put("entiti", entiti);
		
        } catch (Exception e) {
			e.printStackTrace();
		}
		
		return "created";
	}

public String openEntity() {
		
		try {
					
			User user1 = (User) ActionContext.getContext().getSession().get("sesUser");
			role = user1.getRole();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "opened";
	}

	
	
	public static List<Long> getKeysFromValue(Map<Long, String> hm, String value) {
		List<Long> list = new ArrayList<Long>();
		for (Long o : hm.keySet()) {
			if (hm.get(o).equals(value)) {
				list.add(o);
			}
		}
		return list;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;

	}
	
	public Entiti getEntiti() {
		return entiti;
	}

	public void setEntiti(Entiti entiti) {
		this.entiti = entiti;
	}
	
	public int getScreenName() {
		return screenName;
	}

	public void setScreenName(int screenName) {
		this.screenName = screenName;
	}
	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
	
	public Map<String, List<String>> getLedgerMap() {
		return ledgerMap;
	}

	public void setLedgerMap(Map<String, List<String>> ledgerMap) {
		this.ledgerMap = ledgerMap;
	}
	
	
	public List<Ledger> getLedgerList() {
		return ledgerList;
	}

	public void setLedgerList(List<Ledger> ledgerList) {
		this.ledgerList = ledgerList;
	}
	
}
